package com.banshuibao.bsb_android.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.PayModel;
import com.banshuibao.bsb_android.model.SendModel;
import com.banshuibao.bsb_android.util.XToast;
import com.banshuibao.bsb_android.wxapi.WXPayEntryActivity;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class PayActivity extends BaseActivity implements View.OnClickListener {

    private TextView pay_name;
    private TextView money;
    private RelativeLayout weChat, wellat;
    private String content;
    private String price;
    private int flag;
    private static String[] images;
    private String uuid;

    private IWXAPI api;
    private String appId;
    private String partnerid;
    private String prepayid;
    private String extend;
    private String nonceStr;
    private String timeStamp;
    private String sign;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        api = WXAPIFactory.createWXAPI(this, Constants.WXID.APP_ID);
        api.registerApp(Constants.WXID.APP_ID);

        initView();
        getData();
    }

    private void getData() {
        content = getIntent().getStringExtra("content");
        price = getIntent().getStringExtra("price");
        uuid = getIntent().getStringExtra("uuid");
        flag = getIntent().getIntExtra("flag", 0);

        if (flag == 1) {
            pay_name.setText("发布咨询");
        } else if (flag == 2) {
            pay_name.setText("一对一咨询");
        } else if (flag == 3) {
            pay_name.setText("追问");
        } else if (flag == 4) {
            pay_name.setText("视频");
        } else if (flag == 5) {
            pay_name.setText("专家答疑");
        }else if(flag == 6){
            pay_name.setText("会员卡");
        }
        money.setText(price + "元");
    }

    private void initView() {
        setTitleText("购买服务");
        //哪项支付
        pay_name = (TextView) findViewById(R.id.pay_tv);
        //显示的money
        money = (TextView) findViewById(R.id.pay_money);
        //微信支付
        weChat = (RelativeLayout) findViewById(R.id.relative_pay_one);
        weChat.setOnClickListener(this);
        //余额支付
//        wellat = (RelativeLayout) findViewById(R.id.relative_pay_two);
//        wellat.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.relative_pay_one://微信
                showPopupPrompt();
                break;
//            case R.id.relative_pay_two://钱包
//                showPopupPromptTwo();
//                break;
        }
    }

    //广播接收订单号
    public static class shangBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                images = intent.getStringArrayExtra("images");
            }
        }
    }

    //微信支付
    private void showPopupPrompt() {
        View view = View.inflate(this, R.layout.prompt_popup, null);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        Button no = (Button) view.findViewById(R.id.popup_btn_no);
        Button yes = (Button) view.findViewById(R.id.popup_btn_yes);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isWXAppInstalledAndSupported()) {
                    Intent intent = new Intent(PayActivity.this, WXPayEntryActivity.class);
                    showProgressDialog();
                    switch (flag) {
                        case 1:
                            WXPostOne(intent);
                            break;
                        case 2:
                            WXPostTwo(intent);
                            break;
                        case 3:
                            WXPostThree(intent);
                            break;
                        case 4:
                            WXPostFour(intent);
                            break;
                        case 5:
                            WXPostFive(intent);
                            break;
                        case 6:
                            WXPostSix(intent);
                            break;
                    }
                    weChat.setClickable(false);
                    popupWindow.dismiss();
                } else {
                    XToast.showToast(PayActivity.this, "请安装或更新微信最新版本");
                }
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        View parent = View.inflate(PayActivity.this, R.layout.activity_pay, null);
        popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    /**
     * 快速
     * @param intent
     *
     */
    private void WXPostOne(final Intent intent) {
        SendModel.body body = new SendModel.body();
        body.setContent(content);
        body.setPayType("WEIXIN");
        body.setMoney(Double.valueOf(price));
        if (images != null) {
            body.setImages(images);
        }
        HttpUtil.doPost(Constants.Url.SEND_CONSULTTATION, body, new HttpResponse(this, PayModel.class) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                PayModel s = (PayModel) obj;
                PayModel.DataBean data = s.getData();
                appId = data.getAppId();
                partnerid = data.getPartnerid();
                prepayid = data.getPrepayid();
                extend = data.getPackageValue();
                nonceStr = data.getNonceStr();
                timeStamp = data.getTimeStamp();
                sign = data.getSign();
                String orderNo = data.getOrderNo();
                intent.putExtra("flag", "1");
                intent.putExtra("order",orderNo);
                startActivityForResult(intent, 101);
                setPost();
       //         finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                weChat.setClickable(true);
            }
        });
    }

    /**
     * 一对一
     * @param intent
     */
    private void WXPostTwo(final Intent intent) {
        SendModel.body body = new SendModel.body();
        body.setContent(content);
        body.setPayType("WEIXIN");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        if (images != null) {
            body.setImages(images);
        }
        HttpUtil.doPost(Constants.Url.SEND_O2O_QUESTION, body, new HttpResponse(this,PayModel.class) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                PayModel s = (PayModel) obj;
                PayModel.DataBean data = s.getData();
                appId = data.getAppId();
                partnerid = data.getPartnerid();
                prepayid = data.getPrepayid();
                extend = data.getPackageValue();
                nonceStr = data.getNonceStr();
                timeStamp = data.getTimeStamp();
                sign = data.getSign();
                String orderNo = data.getOrderNo();
                intent.putExtra("flag", "2");
                intent.putExtra("order",orderNo);
                startActivityForResult(intent, 102);
                setPost();
            //    finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                weChat.setClickable(true);
            }
        });
    }

    /**
     * 追问
     * @param intent
     */
    private void WXPostThree(final Intent intent) {
        SendModel.body body = new SendModel.body();
        body.setContent(content);
        body.setPayType("WEIXIN");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.SEND_CLOSELY_QUESTION, body, new HttpResponse(this,PayModel.class) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                PayModel s = (PayModel) obj;
                PayModel.DataBean data = s.getData();
                appId = data.getAppId();
                partnerid = data.getPartnerid();
                prepayid = data.getPrepayid();
                extend = data.getPackageValue();
                nonceStr = data.getNonceStr();
                timeStamp = data.getTimeStamp();
                sign = data.getSign();
                String orderNo = data.getOrderNo();
                intent.putExtra("flag", "3");
                intent.putExtra("order",orderNo);
                startActivityForResult(intent, 103);
                setPost();
         //       finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                weChat.setClickable(true);
            }
        });
    }

    /**
     * 视频购买
     * @param intent
     */
    private void WXPostFour(final Intent intent) {
        SendModel.body body = new SendModel.body();
        body.setPayType("WEIXIN");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.VIDEO_BUY, body, new HttpResponse(this,PayModel.class) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                PayModel s = (PayModel) obj;
                PayModel.DataBean data = s.getData();
                appId = data.getAppId();
                partnerid = data.getPartnerid();
                prepayid = data.getPrepayid();
                extend = data.getPackageValue();
                nonceStr = data.getNonceStr();
                timeStamp = data.getTimeStamp();
                sign = data.getSign();
                String orderNo = data.getOrderNo();
                intent.putExtra("flag", "4");
                intent.putExtra("order",orderNo);
                startActivityForResult(intent, 104);
                setPost();
        //        finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                weChat.setClickable(true);
            }
        });
    }

    /**
     * 音频购买
     * @param intent
     */
    private void WXPostFive(final Intent intent) {
        SendModel.body body = new SendModel.body();
        body.setPayType("WEIXIN");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.AUDIO_BUY, body, new HttpResponse(this,PayModel.class) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                PayModel s = (PayModel) obj;
                PayModel.DataBean data = s.getData();
                appId = data.getAppId();
                partnerid = data.getPartnerid();
                prepayid = data.getPrepayid();
                extend = data.getPackageValue();
                nonceStr = data.getNonceStr();
                timeStamp = data.getTimeStamp();
                sign = data.getSign();
                String orderNo = data.getOrderNo();
                intent.putExtra("flag", "5");
                intent.putExtra("order",orderNo);
                startActivityForResult(intent, 105);
                setPost();
            //    finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                weChat.setClickable(true);
            }
        });
    }

    /**
     * 购买365会员卡
     * @param intent
     */
    private void WXPostSix(final Intent intent) {
        SendModel.body body = new SendModel.body();
        body.setPayType("WEIXIN");
        body.setMoney(Double.valueOf(price));
        HttpUtil.doPost(Constants.Url.VIP_BUY, body, new HttpResponse(this,PayModel.class) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                PayModel s = (PayModel) obj;
                PayModel.DataBean data = s.getData();
                appId = data.getAppId();
                partnerid = data.getPartnerid();
                prepayid = data.getPrepayid();
                extend = data.getPackageValue();
                nonceStr = data.getNonceStr();
                timeStamp = data.getTimeStamp();
                sign = data.getSign();
                String orderNo = data.getOrderNo();
                intent.putExtra("flag", "6");
                intent.putExtra("order",orderNo);
                startActivityForResult(intent, 106);
                setPost();
             //   finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                weChat.setClickable(true);
            }
        });
    }

    private void setPost() {
        PayReq req = new PayReq();
        req.appId = this.appId;
        req.partnerId = this.partnerid;
        req.prepayId = this.prepayid;
        req.packageValue = this.extend;
        req.nonceStr = this.nonceStr;
        req.timeStamp = this.timeStamp;
        req.sign = this.sign;
        api.sendReq(req);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    //判断是否安装微信客户端
    private boolean isWXAppInstalledAndSupported() {
        boolean sIsWXAppInstalledAndSupported = api.isWXAppInstalled();
        return sIsWXAppInstalledAndSupported;
    }


    //余额支付
    private void showPopupPromptTwo() {
        View view = View.inflate(this, R.layout.prompt_popup, null);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        Button no = (Button) view.findViewById(R.id.popup_btn_no);
        Button yes = (Button) view.findViewById(R.id.popup_btn_yes);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showProgressDialog();
                switch (flag) {
                    case 1:
                        setSendPost();
                        break;
                    case 2:
                        sendNet();
                        break;
                    case 3:
                        requestNet();
                        break;
                    case 4:
                        sendBuyMedia();
                        break;
                    case 5:
                        buyExpertAnswer();
                        break;
                    case 6:
                        buyVipCard();
                        break;
                }
                wellat.setClickable(false);
                popupWindow.dismiss();
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        View parent = View.inflate(PayActivity.this, R.layout.activity_pay, null);
        popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    private void setSendPost() {
        SendModel.body body = new SendModel.body();
        body.setContent(content);
        body.setPayType("BALANCE");
        body.setMoney(Double.valueOf(price));
        if (images != null) {
            body.setImages(images);
        }
        HttpUtil.doPost(Constants.Url.SEND_CONSULTTATION, body, new HttpResponse(this, SendModel.class) {
            @Override
            public void onResult(Object obj) {
                SendModel s = (SendModel) obj;
//                LogUtil.e(s);
                cancelProgressDialog();
                XToast.showToast(PayActivity.this, "发布咨询成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
           //     wellat.setClickable(true);
                cancelProgressDialog();
            }
        });
    }

    /**
     * 追问发布接口
     */
    public void requestNet() {
        SendModel.body body = new SendModel.body();
        body.setContent(content);
        body.setPayType("BALANCE");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.SEND_CLOSELY_QUESTION, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                cancelProgressDialog();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
        //        wellat.setClickable(true);
                cancelProgressDialog();
            }
        });
    }

    /**
     * 一对一发布接口
     */
    public void sendNet() {
        SendModel.body body = new SendModel.body();
        body.setContent(content);
        body.setPayType("BALANCE");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        if (images != null) {
            body.setImages(images);
        }
        HttpUtil.doPost(Constants.Url.SEND_O2O_QUESTION, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                cancelProgressDialog();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
       //         wellat.setClickable(true);
                cancelProgressDialog();
            }
        });
    }

    /**
     * 购买视频
     */
    public void sendBuyMedia() {
        SendModel.body body = new SendModel.body();
        body.setPayType("BALANCE");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.VIDEO_BUY, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                cancelProgressDialog();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
       //         wellat.setClickable(true);
                cancelProgressDialog();
            }
        });
    }

    /**
     * 购买专家答疑
     */
    public void buyExpertAnswer() {
        SendModel.body body = new SendModel.body();
        body.setPayType("BALANCE");
        body.setMoney(Double.valueOf(price));
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.AUDIO_BUY, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                cancelProgressDialog();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
       //         wellat.setClickable(true);
                cancelProgressDialog();
            }
        });
    }

    /**
     * 购买365会员卡
     */
    public void buyVipCard() {
        SendModel.body body = new SendModel.body();
        body.setPayType("BALANCE");
        body.setMoney(Double.valueOf(price));
        HttpUtil.doPost(Constants.Url.VIP_BUY, body, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
                cancelProgressDialog();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
      //          wellat.setClickable(true);
                cancelProgressDialog();
            }
        });
    }
}
